﻿<!DOCTYPE HTML>
<html lang="zh">
<head>
<title>ControlSend[Text] - 语法 &amp; 使用 | AutoHotkey v2</title>
<meta name="description" content="The ControlSend and ControlSendText functions send simulated keystrokes or text to a window or control." />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
<script type="text/javascript">$(function(){0<=window.navigator.userAgent.toLowerCase().indexOf("ucbrowser")&&CaoNiMaDeUc()})</script>
</head>
<body>

<h1>ControlSend / ControlSendText</h1>

<p>Sends simulated keystrokes or text to a window or control.</p>

<pre class="Syntax"><span class="func">ControlSend</span> Keys <span class="optional">, Control, WinTitle, WinText, ExcludeTitle, ExcludeText</span>
<span class="func">ControlSendText</span>: Same parameters as above.</pre>
<h2 id="Parameters">参数</h2>
<dl>

  <dt>Keys</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>The sequence of keys to send (see the <a href="Send.htm">Send</a> function for details). The rate at which characters are sent is determined by <a href="SetKeyDelay.htm">SetKeyDelay</a>.</p>
    <p>Unlike the <a href="Send.htm">Send</a> function, mouse clicks cannot be sent by ControlSend. Use <a href="ControlClick.htm">ControlClick</a> for that.</p>
  </dd>

  <dt>Control</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a>, <a href="../Concepts.htm#numbers">整数</a>或<a href="../Objects.htm">对象</a></p>
    控件的 ClassNN, 文本或 HWND, 或具有 <code>Hwnd</code> 属性的对象. 有关详情, 请参阅 <a href="Control.htm#Parameter">Control 参数</a>.</p>
    <p>If this parameter is omitted, the keystrokes will be sent directly to the target window instead of one of its controls (see <a href="../misc/Winamp.htm">Automating Winamp</a> for an example).</p>
  </dd>

  <dt>WinTitle</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a>, <a href="../Concepts.htm#numbers">整数</a>或<a href="../Objects.htm">对象</a></p>
    <p>识别目标窗口的窗口标题或其他条件. 请参阅 <a href="../misc/WinTitle.htm">WinTitle</a>.</p>
  </dd>

  <dt>WinText</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>如果存在, 此参数必须是目标窗口的单个文本元素的子字符串(和内置的 Window Spy 工具显示的一样). 如果 <a href="DetectHiddenText.htm">DetectHiddenText</a> 为 ON, 那么会检测隐藏文本元素.</p>
  </dd>

  <dt>ExcludeTitle</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>标题中含有此参数值的窗口将被排除.</p>
  </dd>

  <dt>ExcludeText</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>文本中含有此参数值的窗口将被排除.</p>
  </dd>

</dl>

<h2 id="Error_Handling">错误处理</h2>
<p>如果遇到问题, <a href="../misc/ErrorLevel.htm">ErrorLevel</a> 设置为 1, 否则设置为 0.</p>
<h2 id="Remarks">备注</h2>
<p>ControlSendText sends the individual characters of the <em>Keys</em> parameter without translating <code>{Enter}</code> to <kbd>Enter</kbd>, <code>^c</code> to <kbd>Control</kbd>+<kbd>C</kbd>, etc. For details, 请参阅 <a href="Send.htm#SendText">Text mode</a>. It is also valid to use <a href="Send.htm#Raw">{Raw}</a> or <a href="Send.htm#Text">{Text}</a> with ControlSend.</p>
<p>If the <em>Control</em> parameter is omitted, this function will attempt to send directly to the target window by sending to its topmost control (which is often the correct one) or the window itself if there are no controls. This is useful if a window does not appear to have any controls at all, or just for the convenience of not having to worry about which control to send to.</p>
<p>By default, modifier keystrokes (<kbd>Control</kbd>, <kbd>Alt</kbd>, <kbd>Shift</kbd>, and <kbd>Win</kbd>) are sent as they normally would be by the Send function. This allows command prompt and other console windows to properly detect uppercase letters, control characters, etc. It may also improve reliability in other ways.</p>
<p>However, in some cases these modifier events may interfere with the active window, especially if the user is actively typing during a ControlSend or if the <kbd>Alt</kbd> key is being sent (since <kbd>Alt</kbd> activates the active window's menu bar). This can be avoided by explicitly sending modifier up and down events as in this example:</p>
<pre>ControlSend "{Alt down}f{Alt up}", "Edit1", "Untitled - Notepad"</pre>
<p>The method above also allows the sending of modifier keystrokes (<kbd>Control</kbd>/<kbd>Alt</kbd>/<kbd>Shift</kbd>/<kbd>Win</kbd>) while the workstation is locked (protected by logon prompt).</p>
<p><a href="BlockInput.htm">BlockInput</a> should be avoided when using ControlSend against a console window such as command prompt. This is because it might prevent capitalization and modifier keys such as <kbd>Control</kbd> from working properly.</p>
<p>The value of <a href="SetKeyDelay.htm">SetKeyDelay</a> determines the speed at which keys are sent. If the target window does not receive the keystrokes reliably, try increasing the press duration via the second parameter of <a href="SetKeyDelay.htm">SetKeyDelay</a> as in these examples:</p>
<pre>SetKeyDelay 10, 10
SetKeyDelay 0, 10
SetKeyDelay -1, 0</pre>
<p>If the target control is an Edit control (or something similar), the following are usually more reliable and faster than ControlSend:</p>
<pre><a href="ControlEditPaste.htm">ControlEditPaste</a>("This text will be inserted at the caret position.", ControlName, WinTitle)</pre>
<pre><a href="ControlSetText.htm">ControlSetText</a>("This text will entirely replace any current text.", ControlName, WinTitle)</pre>
<p>ControlSend is generally not capable of manipulating a window's menu bar. To work around this, use <a href="MenuSelect.htm">MenuSelect</a>. If that is not possible due to the nature of the menu bar, you could try to discover the message that corresponds to the desired menu item by following the <a href="../misc/SendMessage.htm">SendMessage Tutorial</a>.</p>
<p>窗口标题和文本是区分大小写的. 除非 <a href="DetectHiddenWindows.htm">DetectHiddenWindows</a> 被打开, 否则不会检测隐藏窗口.</p>
<h2 id="Related">相关</h2>
<p><a href="SetKeyDelay.htm">SetKeyDelay</a>, <a href="../misc/EscapeChar.htm">Escape sequences (e.g. `n) </a>, <a href="Control.htm">Control functions</a>, <a href="Send.htm">Send</a>, <a href="../misc/Winamp.htm">Automating Winamp</a></p>
<h2 id="Examples">示例</h2>
<div class="ex" id="ExNotepad">
<p><a href="#ExNotepad">#1</a>: 打开记事本(最小化的) 并发送一些文本.</p>
<pre>Run "Notepad",, "Min", PID  <em>; 最小化运行记事本.</em>
WinWait "ahk_pid " PID  <em>; 等待记事本进程的出现.</em>
<em>; 将文本发送到不活动的记事本编辑控件.
; 省略了第三个参数, 因此使用最后一个找到的窗口.</em>
ControlSend "This is a line of text in the notepad window.{Enter}", "Edit1"
ControlSendText "Notice that {Enter} is not sent as an Enter keystroke with ControlSendText.", "Edit1"

Msgbox "Press OK to activate the window to see the result."
WinActivate "ahk_pid " PID  <em>; Show the result.</em></pre>
</div>

<div class="ex" id="ExCmd">
<p><a href="#ExCmd">#2</a>: 打开命令提示符并向它发送一些文本.</p>
<pre>SetTitleMatchMode 2
Run A_ComSpec,,, PID  <em>; 打开命令提示符.</em>
WinWait "ahk_pid " PID  <em>; 等待它的出现.</em>
ControlSend "ipconfig{Enter}",, "cmd.exe"  <em>; 直接发送到命令提示符窗口.</em></pre>
</div>

</body>
</html>